package pcircles.android.study;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.content.Intent;
import android.content.pm.*;
import android.content.pm.PackageManager.NameNotFoundException;
import android.widget.TextView;
import java.util.*;

public class AppPrivacySettingActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_permissions);

		final TextView permissionsList = (TextView) findViewById(R.id.permissionsList);

		PackageManager pkgManager = permissionsList.getContext()
				.getPackageManager();
		Intent intent = getIntent();
		String packageName = intent.getStringExtra(MainActivity.APP_NAME);
		
		try {
			PackageInfo pkgInfo = pkgManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS
							| PackageManager.GET_PROVIDERS);
			permissionsList.append("Permissions for: "+pkgInfo.applicationInfo.loadLabel(pkgManager)+"\n\n");
			if (pkgInfo.requestedPermissions != null) {
				for (String permission : pkgInfo.requestedPermissions) {
					
					PermissionInfo pinfo = pkgManager.getPermissionInfo(
								permission, PackageManager.GET_META_DATA);
					CharSequence label = pinfo.loadLabel(pkgManager);
					CharSequence desc = pinfo.loadDescription(pkgManager);
					permissionsList.append("NAME: " + label
							+ ", \nDESCRIPTION: " + desc + "\n\n");
				}
				permissionsList.setMovementMethod(new ScrollingMovementMethod());
			} else {
				permissionsList.append("No permissions found");
			}

		} catch (NameNotFoundException e1) {
			permissionsList.append("failed2");
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}